package com.justyh.ssm.util;

import com.justyh.ssm.dao.PlaceDao;
import com.justyh.ssm.model.Place;
import com.justyh.ssm.service.PlaceService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created by yh on 2016/9/17.
 */
public class JsoupGetPlace {
    @Autowired
    private PlaceService placeService;
   @Resource
   private PlaceDao placeDao;
  /* public static void main(String[] args) throws IOException {
       *//* List<String> list=new ArrayList<String>();
        Document doc= Jsoup.connect("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2015")
                .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                .timeout(50000).get();
        // System.out.println(doc.getAllElements());


        Elements provinces=doc.getElementsByClass("provincetr");
        for(Element provinceElement:provinces){
            Elements provinceSelect=provinceElement.select("a");
            for(Element provience:provinceSelect){
                //System.out.println(provience+parseNext(provience));
                list.add(provience.text()+"："+parseCity(provience));
            }
        }
        for(String city:list){
            System.out.println(city);
        }*//*
//       JsoupGetPlace jg=new JsoupGetPlace();
//       jg.getPlaces("fuck");

    }*/
    @Test
    @Rollback(false)
    public  void getPlaces(){
        try{
            Document doc= Jsoup.connect("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2015")
                    .userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                    .timeout(50000).get();
            Elements provinces=doc.getElementsByClass("provincetr");
            for(Element provinceElement:provinces){
                Elements provinceSelect=provinceElement.select("a");
                for(Element provience:provinceSelect){
                    //System.out.println(provience+parseNext(provience));
                    // list.add(provience.text()+"："+parseCity(provience));
                    parseCity(provience);
                }
            }
        }catch (Exception e){
           e.printStackTrace();
        }


    }
    public  void parseCity(Element parseElement) throws IOException {
        StringBuffer citys=new StringBuffer();
        Document document=Jsoup.connect(parseElement.attr("abs:href")).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                .timeout(50000).get();
        Elements cityElements=document.getElementsByClass("citytr");
        for(Element cityElement:cityElements){
//            if(parseElement.text().toString().endsWith("市")){
//                citys.append(parseCounty(cityElement.select("a").last()));
//            }else{
               // citys.append(cityElement.select("a").last().text()+" ");
           // }
            parseCounty(cityElement.select("a").last(),parseElement.text());
        }
       // return citys.toString();

    }
    public  void parseCounty(Element element,String province) throws IOException {
        StringBuffer countys=new StringBuffer();
        Document document=Jsoup.connect(element.attr("abs:href")).userAgent("Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.15)")
                .timeout(50000).get();
        Elements countyElements=document.getElementsByClass("countytr");
        for(Element countyElement:countyElements){
           // countys.append(countyElement.select("a").last().text()+" ");
            //countys.append(countyElement);
            Place place=new Place();
            place.setProvince(province);
            place.setCity(element.text());
            place.setCounty(countyElement.select("a").last().text());
            placeDao.insert(place);
        }
        //return countys.toString();
    }
}
